    package com.erbao.thread.juc;

    import com.erbao.thread.TreadInterrupt;

    import java.util.concurrent.Exchanger;

    /**
     * @author CaoBaoYin
     * @since 2022/2/26 19:29
     */
    public class ExchangeTest {

        private static Exchanger<String> exchanger = new Exchanger<>();

        public static void main(String[] args) {
            Thread t1 = new Thread(() -> {
                System.out.println("t1 start");
                String s = null;
                try {
                    s = exchanger.exchange("t1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName().concat(":").concat(s));
            }, "t1");
            Thread t2 = new Thread(() -> {
                System.out.println("t2 start");
                String s = null;
                try {
                    s = exchanger.exchange("t2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName().concat(":").concat(s));
            }, "t2");

            t1.start();
            t2.start();
        }
    }
